import {
    getShopFreightTemplateAllData,
} from "@Api/shopsApi.js"

export function getshopFreightTemplateFarmet(pra: { page: number, pageSize: number }) {
    let GoodsDataCache:any = {}
    let goodsResolve: any = null
    return getFlashWithGoodsData(pra)
    function getFlashWithGoodsData(data: any) {
        return new Promise((resolve, reject) => {
            goodsResolve = goodsResolve || resolve
            getShopFreightTemplateAllData(data).then((res: any) => {
                for(let key in res.data) {
                    GoodsDataCache[key] = res.data[key]
                }
                if (res.total > (data.page * data.pageSize)) {
                    data.page++
                    getFlashWithGoodsData(data)
                } else {
                    for (let key in GoodsDataCache) {
                        GoodsDataCache[key].yunfeiconterlist = (() => {
                            const outerconte = {
                                typeinder: key == '0', // true 全国包邮
                                fahuodi:
                                    key == '0'
                                        ? GoodsDataCache[key].area
                                        : `${GoodsDataCache[key].product_province_name}${GoodsDataCache[key].product_city_name}`,
                                list: (() => {
                                    if (key == '0') {
                                        return []
                                    }
                                    const { rule_type, columns } = GoodsDataCache[key]
                                    const outerlistcon = [] // {diqu: '', temperu: ''}
                                    switch (rule_type) {
                                        case 0:
                                            for (let ckey in columns) {
                                                const columnsdaya = columns[ckey]
                                                if (ckey == '1') {
                                                    outerlistcon.push({
                                                        diqu: '默认',
                                                        temperu: `${columnsdaya.first_num}件以内,${columnsdaya.first_num_price}元每增加${columnsdaya.add_num}件,增加运费${columnsdaya.add_num_price}元`,
                                                    })
                                                }
                                                if (ckey == '2') {
                                                    outerlistcon.push({
                                                        diqu: columnsdaya.province_names.join('、'),
                                                        temperu: `${columnsdaya.first_num}件以内,${columnsdaya.first_num_price}元每增加${columnsdaya.add_num}件,增加运费${columnsdaya.add_num_price}元`,
                                                    })
                                                }
                                                if (ckey.length === 18) {
                                                    outerlistcon.push({
                                                        diqu: columnsdaya.province_names.join('、'),
                                                        temperu: `限制下单`,
                                                    })
                                                }
                                                if (ckey == '3') {
                                                    const unittxt = columnsdaya.over_num
                                                        ? '件'
                                                        : '元'
                                                    const unitNum = columnsdaya.over_amount
                                                        ? (columnsdaya.over_amount / 100).toFixed(2)
                                                        : columnsdaya.over_amount
                                                    outerlistcon.push({
                                                        diqu: columnsdaya.province_names.join('、'),
                                                        temperu: `满${unitNum}${unittxt},包邮`,
                                                    })
                                                }
                                            }
                                            break
                                        case 1:
                                            outerlistcon.push({
                                                diqu: '默认',
                                                temperu: `固定运费${
                                                    (GoodsDataCache[key].fixed_amount / 100).toFixed(2) * 1
                                                }元`,
                                            })
                                            for (let ckey in columns) {
                                                const columnsdaya = columns[ckey]
                                                if (ckey === '1') {
                                                    const unittxt = columnsdaya.over_num
                                                        ? '件'
                                                        : '元'
                                                    const unitNum = columnsdaya.over_amount
                                                        ? (columnsdaya.over_amount / 100).toFixed(2)
                                                        : columnsdaya.over_amount
                                                    outerlistcon.push({
                                                        diqu: columnsdaya.province_names.join('、'),
                                                        temperu: `满${unitNum}${unittxt},包邮`,
                                                    })
                                                }
                                                if (ckey.length === 18) {
                                                    outerlistcon.push({
                                                        diqu: columnsdaya.province_names.join('、'),
                                                        temperu: `限制下单`,
                                                    })
                                                }
                                            }
                                            break
                                        case 2:
                                            for (let ckey in columns) {
                                                const columnsdaya = columns[ckey]
                                                outerlistcon.push({
                                                    diqu: '默认',
                                                    temperu: `包邮`,
                                                })
                                                if (ckey.length === 18) {
                                                    outerlistcon.push({
                                                        diqu: columnsdaya.province_names.join('、'),
                                                        temperu: `限制下单`,
                                                    })
                                                }
                                            }
                                            break
                                        default:
                                            break
                                    }
                                    return outerlistcon
                                })(),
                            }
                            return outerconte
                        })()
                    }
                    goodsResolve(GoodsDataCache)
                }

            }).catch(err => {
                console.log('err', err)
            })
        })
    }
}